package com.mzj.saas.mservice.property.controller;


import com.mzj.saas.annotation.CheckAuth;
import com.mzj.saas.annotation.RepeatSubmit;
import com.mzj.saas.commons.ApiData;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.house.entity.House;
import com.mzj.saas.mservice.property.entity.CommunityHouseUser;
import com.mzj.saas.mservice.property.service.CommunityHouseUserService;
import com.mzj.saas.mservice.property.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 成员管理
 *
 * @author hikari
 * @since 2021-05-25
 */
@RestController
@RequestMapping("/property/communityHouseUser")
@Api(tags = "成员管理")
public class CommunityHouseUserController {
    @Autowired
    private CommunityHouseUserService communityHouseUserService;


    /**
     * 房产成员列表
     *
     * @param accessToken
     * @param searchCondition 搜索条件
     * @param houseId         房产id
     * @param communityId     小区
     * @param buildId         楼栋
     * @param unitId          单元
     * @param roomNumber      房号
     * @param floor           楼层
     * @param name            产权人
     * @param mobile          产权人电话
     * @param type            成员人数
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResultBean<ApiData<CommunityHouseUserVO>> list(@RequestHeader String accessToken,
                                                          @RequestParam(required = false) Integer determineType,
                                                          @RequestParam(required = false) String searchCondition, @RequestParam(required = false) String houseId,
                                                          @RequestParam(required = false) String communityId, @RequestParam(required = false) Long buildId,
                                                          @RequestParam(required = false) Long unitId, @RequestParam(required = false) String roomNumber,
                                                          @RequestParam(required = false) Integer floor, @RequestParam(required = false) String name,
                                                          @RequestParam(required = false) String mobile, @RequestParam(required = false) Integer type,
                                                          @RequestParam(required = false) String openPushMessageType,
                                                          @RequestParam(defaultValue = "0") Integer pageNumber, @RequestParam(defaultValue = "5") Integer pageSize) {
        return communityHouseUserService.list(accessToken, determineType, searchCondition, houseId, communityId, buildId, unitId,
                roomNumber, floor, name, mobile, type, openPushMessageType, pageNumber, pageSize);
    }


    /**
     * 新增/修改保存成员
     *
     * @param accessToken
     * @param accessToken,SaveOrEditVO
     * @return
     * @author shun
     * @date 2021年6月4日
     */
    @PostMapping
    @RepeatSubmit
    @ApiOperation("新增/修改成员（同住人）")
    public ResultBean<Object> saveOrEdit(@RequestHeader String accessToken, @RequestBody SaveOrEditVO saveOrEditVO) {
        return communityHouseUserService.saveOrEdit(accessToken, saveOrEditVO);
    }


    /**
     * 新增同住人信息并进行实名认证
     * 二要素身份校验，无手机号、人脸上传
     * @param accessToken
     * @param accessToken,SaveOrEditVO
     * @return
     * @author shun
     * @date 2021年6月4日
     */
    @PostMapping("/eidAuthentication")
    @RepeatSubmit
    public ResultBean<Object> eidAuthentication(@RequestHeader String accessToken, @RequestBody CommunityHouseUser communityHouseUser) {
        return communityHouseUserService.eidAuthentication(accessToken, communityHouseUser);
    }

    /**
     * 新增同住人信息并进行实名认证
     * 三要素身份校验-二要素校验
     * @param accessToken
     * @param accessToken,SaveOrEditVO
     * @return
     * @author shun
     * @date 2021年6月4日
     */
    @PostMapping("/addUserByOrder")
    @RepeatSubmit
    public ResultBean<Object> addUserByOrder(@RequestHeader String accessToken, @RequestBody CommunityHouseUser communityHouseUser) {
        return communityHouseUserService.addUserByOrder(accessToken, communityHouseUser);
    }

    @GetMapping("/getUserInfo/{id}")
    public ResultBean<Object> getUserInfo(@RequestHeader String accessToken, @PathVariable Long id){
        return communityHouseUserService.getUserInfo(accessToken,id);
    }

    /**
     * 编辑同住人
     *
     * @param accessToken
     * @return
     * @author shun
     * @date 2021年6月4日
     */
    @PutMapping("/editUserByOrder")
    @RepeatSubmit
    public ResultBean<Object> editUserByOrder(@RequestHeader String accessToken,
                                                       @RequestBody CommunityHouseUser communityHouseUser) {
        return communityHouseUserService.editUserByOrder(accessToken, communityHouseUser);
    }

    /**
     * 删除同住人
     *
     * @param accessToken
     * @param id
     * @return
     * @author shun
     * @date 2021年6月4日
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除同住人")
    public ResultBean<Object> delete(@RequestHeader String accessToken, @PathVariable Long id) {
        return communityHouseUserService.delete(accessToken, id);
    }

    /**
     * 编辑
     *
     * @param accessToken
     * @param communityHouseUsers
     * @return
     * @author shun
     * @date 2021年6月4日
     */
    @PutMapping
    @RepeatSubmit
    public ResultBean<List<CommunityHouseUserVO>> edit(@RequestHeader String accessToken,
                                                       @Valid @RequestBody List<CommunityHouseUser> communityHouseUsers) {
        return communityHouseUserService.edit(accessToken, communityHouseUsers);
    }

    /**
     * 查询房屋成员列表
     *
     * @param id
     * @return
     */
    @GetMapping("/select/{id}")
    @ApiOperation("查询房屋信息和成员列表")
    public ResultBean<ApiData<ReturnResultVO>> findCommunityHouseUsersbyId(@RequestHeader String accessToken, @PathVariable String id) {
        return communityHouseUserService.findCommunityHouseUsersbyId(accessToken, id);
    }


    /**
     * 根据姓名或手机号模糊查询
     */
    @GetMapping("/select/nameOrphone/{keyword}")
    public ResultBean<ApiData<MatchStockUserVo>> findCommunityHouseUserByNameOrPhone(@PathVariable String keyword) {
        return communityHouseUserService.findCommunityHouseUserByNameOrPhone(keyword);
    }


    /**
     * 根据单位名称和统一社会信用征信模糊查询
     */
    @GetMapping("/select/companyNameOrsocialCreditNumber")
    public ResultBean<ApiData<MatchUserHouseVO>> findMatchUserByCompanyNameOrSocialCreditNumber(MatchUserHouseVO vo) {
        return communityHouseUserService.findMatchUserByCompanyNameOrSocialCreditNumber(vo);
    }

    /**
     * 非公租房成员信息--导出
     */
    @GetMapping("/communityHouseExport")
    public void communityHouseExport(@RequestHeader String accessToken, HttpServletResponse response,
                                     @RequestParam(required = false) Integer determineType,
                                     @RequestParam(required = false) String searchCondition, @RequestParam(required = false) String houseId,
                                     @RequestParam(required = false) String communityId, @RequestParam(required = false) Long buildId,
                                     @RequestParam(required = false) Long unitId, @RequestParam(required = false) String roomNumber,
                                     @RequestParam(required = false) Integer floor, @RequestParam(required = false) String name,
                                     @RequestParam(required = false) String mobile, @RequestParam(required = false) Integer type,
                                     @RequestParam(required = false) Integer pageNumber, @RequestParam(required = false) Integer pageSize) throws Exception {
        communityHouseUserService.communityHouseExport(accessToken, response, determineType, searchCondition, houseId, communityId, buildId, unitId, roomNumber, floor, name, mobile, type, pageNumber, pageSize);
    }

    /**
     * @param accessToken
     * @param determineType   导出标识    1 正常不导出   2 导出
     * @param searchCondition
     * @param houseId
     * @param communityId
     * @param buildId
     * @param unitId
     * @param roomNumber
     * @param floor
     * @param name
     * @param mobile
     * @param type
     * @param pageNumber
     * @param pageSize
     */
    @GetMapping("/memberHouseExport")
    public void memberHouseExport(@RequestHeader String accessToken, HttpServletResponse response,
                                  @RequestParam(required = false) Integer determineType,
                                  @RequestParam(required = false) String searchCondition, @RequestParam(required = false) String houseId,
                                  @RequestParam(required = false) String communityId, @RequestParam(required = false) Long buildId,
                                  @RequestParam(required = false) Long unitId, @RequestParam(required = false) String roomNumber,
                                  @RequestParam(required = false) Integer floor, @RequestParam(required = false) String name,
                                  @RequestParam(required = false) String mobile, @RequestParam(required = false) Integer type,
                                  @RequestParam(required = false) String openPushMessageType,
                                  @RequestParam(defaultValue = "0") Integer pageNumber, @RequestParam(defaultValue = "5") Integer pageSize) throws Exception {
        communityHouseUserService.memeberHouseExport(accessToken, response, determineType, searchCondition, houseId, communityId, buildId, unitId, roomNumber, floor, name, mobile, type,openPushMessageType, pageNumber, pageSize);

    }

    /**
     * 查询出有承租单位标签的产权机构
     *
     * @param accessToken
     * @return
     */
    @GetMapping("/Unitlessee")
    @ApiOperation("查询出有承租单位标签的产权机构")
    public ResultBean<Object> Unitlessee(@RequestHeader String accessToken) {
        return communityHouseUserService.Unitlessee(accessToken);

    }

    /**
     * 查询合租房屋下的房屋成员列表
     *
     * @param houseId
     * @return
     */
    @GetMapping("/selectList")
    @ApiOperation("查询合租房屋下的房屋成员列表")
    public ResultBean<List<CommunityHouseUserVO>> selectList(@RequestHeader String accessToken,
                                                             @RequestParam(required = false) String houseId,
                                                             @RequestParam(required = false) String roomNumber) {
        return communityHouseUserService.selectList(accessToken, houseId, roomNumber);
    }


    /**
     * 根据用户id查询名下房屋可人脸授权房屋
     */
    @GetMapping("/selectHouseListByUserId")
    public ResultBean<List<House>> selectHouseListByUserId(@RequestHeader String accessToken) {
        return communityHouseUserService.selectHouseListByUserId(accessToken);
    }

    @PutMapping("/updateopenPushMessageType")
    @RepeatSubmit
    @CheckAuth
    public ResultBean<String> updateopenPushMessageType(@RequestHeader String accessToken,@RequestBody CommunityHouseUser communityHouseUser) {
        return communityHouseUserService.updateopenPushMessageType(accessToken,communityHouseUser);
    }



}
